$(function(){
	$("#DepartmentWinId").window('close');
	$("#updateDepartmentWinId").window('close');
	
	/* 地区 图标 */
    var roleToo2 = [{
    	text:'新增',
    	iconCls:'icon-add',
    	handler:function(){
    		companyWin(1);
    	}
    },'-',{
    	text:'修改',
    	iconCls:'icon-edit',
    	handler:function(){
    		update(1);
    	}
    },'-',{
    	text:'删除',
    	iconCls:'icon-cut',
    	handler:function(){
    		delDepartment(1);
    	}
    }];
    
	$('#dgArea').datagrid({
	    url:'../../Areadepartment/queryAreaDepartment',
	    rownumbers:true,
	    singleSelect:true,
	    pagination:true,
	    method:'post',
	    queryParams: {
			parentId:'1',
	    	type:typeid,
	    	isDel:'1'
		},
		toolbar:roleToo2,
		onClickRow: function (rowIndex, rowData) {
			$('#dgDepartment').datagrid('loadData',{total:0,rows:[]}); 
			$('#dgcontent').datagrid('loadData',{total:0,rows:[]}); 
            var id = rowData.id;
            $('#dgDepartment').datagrid({    
    		    url:'../Areadepartment/queryAreaDepartment',
    		    rownumbers:true,
    		    singleSelect:true,
    		    pagination:true,
    		    method:'post',
    		    queryParams: {
            	parentId:id,
    		    	isDel:'1'
    			},
    			toolbar:roleToo,
    			onClickRow: function (rowIndex, rowData) {
    				$('#dgcontent').datagrid('loadData',{total:0,rows:[]}); 
    				var areaId = rowData.id;
    				$('#dgcontent').datagrid({    
    				    url:'../GsController/selectGsHtml',
    				    rownumbers:true,
    				    singleSelect:true,
    				    pagination:true,
    				    method:'post',
    				    queryParams: {
    						areaId:areaId,
    				    	isDel:'1'
    					},
    				    toolbar: [{
    				    	text:'新增',
    				    	iconCls:'icon-add',
    				    	handler:function(){
    				    		addupdateGshtml("add",areaId);
    				    		$('#dgcontent').datagrid("reload");
    				    	}
    				    },'-',{
    				    	text:'修改',
    				    	iconCls:'icon-edit',
    				    	handler:function(){
    				    		addupdateGshtml("edit",areaId);
    				    		$('#dgcontent').datagrid("reload");
    				    	}
    				    }],
    				    columns:[[
    						{field:'title',title:'标题',width:'35%',align:'center'},
    						{field:'summary',title:'来源',width:'23%',align:'center'},
    				        {field:'createTime',title:'发表时间',width:'20%',align:'center',
    				        	formatter: function(value,index){
    				        		var now = new Date(value);
    				    			var time= now.format("yyyy-MM-dd");
    				    			return time;
    							}
    				        },{field:'state',title:'显示/隐藏',width:'20%',align:'center',
    				        	formatter: function(value,row,index){
    				        	
    				        	var sysid="\""+row.id+"\"";
	    				        	if(value=='1'){
	    				        		return "<span><a onclick='qidel("+sysid+",1);'  style='text-decoration:none;color:green' href='#'>已 显 示 </a> </span>";
	    				        	}
	    				        	if(value=='2'){
	    				        		return "<span><a onclick='qidel("+sysid+",2);'  style='text-decoration:none;color:red' href='#'>已 隐 藏 </a> </span>";
	    				        	}
    							}
    						}
    				    ]]    
    				});
    				var p3 = $('#dgcontent').datagrid('getPager');

    	     	    p3.pagination({
    	     	        beforePageText: '',//页数文本框前显示的汉字 
    	     	        afterPageText: '',
    	     	        layout:['first','prev','list','next','last'],
    	     	        displayMsg: '第{from}至{to}条  共{total}条数据',
    	     	    });
    			},
    		    columns:[[       
    		        {field:'departmentName',title:'部门名称',width:'97%',align:'center'}
    		    ]]    
    		});
            var p2 = $('#dgDepartment').datagrid('getPager');

     	    p2.pagination({
     	        beforePageText: '',//页数文本框前显示的汉字 
     	        afterPageText: '',
     	        layout:['first','prev','list','next','last'],
     	        displayMsg: '第{from}至{to}条  共{total}条数据',
     	    });
        }, 
       
	    columns:[[       
	        {field:'departmentName',title:'地区名称',width:'96%',align:'center'} 
	    ]]    
	});
	
	  var p1 = $('#dgArea').datagrid('getPager');

	   p1.pagination({
	        beforePageText: '',//页数文本框前显示的汉字 
	        afterPageText: '',
	        layout:['first','prev','list','next','last'],
	        displayMsg: '第{from}至{to}条  共{total}条数据',
	    });
	   
	   $('#dgDepartment').datagrid({    
		    url:'',
		    columns:[[       
		        {field:'departmentName',title:'部门名称',width:'97%',align:'center'}
		    ]]    
		});
	    /* 部门 图标 */
	    var roleToo = [{
	    	text:'新增',
	    	iconCls:'icon-add',
	    	handler:function(){
	    		companyWin(2);
	    	}
	    },'-',{
	    	text:'修改',
	    	iconCls:'icon-edit',
	    	handler:function(){
	    		update(2);
	    	}
	    },'-',{
	    	text:'删除',
	    	iconCls:'icon-cut',
	    	handler:function(){
	    		delDepartment(2);
	    	}
	    }];
	    
	    /* 正文 图标
	    var roleToo3 = [{
	    	text:'新增',
	    	iconCls:'icon-add',
	    	handler:function(){
	    		addupdateGshtml("add",id);
	    	}
	    },'-',{
	    	text:'修改',
	    	iconCls:'icon-edit',
	    	handler:function(){
	    		addupdateGshtml("edit",id);
	    	}
	    },'-',{
	    	text:'删除',
	    	iconCls:'icon-cut',
	    	handler:function(){
	    	
	    	}
	    }]; */
	    
	    $('#dgcontent').datagrid({    
		    url:'',
		   // toolbar:roleToo,
		    columns:[[       
					{field:'title',title:'标题',width:'35%',align:'center'},
					{field:'summary',title:'来源',width:'23%',align:'center'},
					{field:'createTime',title:'发表时间',width:'25%',align:'center'/*,
						formatter: function(value,row,index){
							var now = new Date(value);
							var time= now.format("yyyy-MM-dd");
							return time;
						}*/
					},{field:'state',title:'显示/隐藏',width:'20%',align:'center'/*,
						formatter: function(value,row,index){
						if(value=='1'){
							return '<span><a href="#" style="text-decoration:none;color:green" onclick=qidel('+row.id+','+value+'); >已 显 示</a></span>';
						}
						if(value=='2'){
							return '<span><a href="#" style="text-decoration:none;color:red" onclick=qidel('+row.id+','+value+'); >已 隐 藏</a></span>';
						}
					}*/
					}
		    ]]    
		});
	    
	    
})
//删除部门信息（逻辑删除）
function delDepartment(td){
	if(td==1){
		var dg = $('#dgArea').datagrid('getSelected');//部门信息
	}else if(td==2){
		var dg = $('#dgDepartment').datagrid('getSelected');//部门信息
	}
	
	if(dgDepartment==null){
		$.messager.alert("提示","请选择部门","error");
	    return;
	}
	$.messager.confirm('确认','您确认想要删除记录吗？',function(r){    
	    if (r){    
	    	var id = dg.id;
	    	$.ajax({  
	    		url:'../Areadepartment/updateAreaDepartment',
	    		type:'post',
	    		data:{
	    		"id":id,
	    		"isDel":'2'
	    		},
	    		dataType:'json',
	    		error:function(){
	    			$.messager.alert("提示","操作异常","error");
	    		}, 
	    		success:function(data){
	    			var row = data.row;
	    			if(row>=0){
	    				$.messager.alert("提示","删除成功","info");
	    				$('#dgArea').datagrid("reload");
	    				$('#dgDepartment').datagrid("reload");
	    				$("#updateCompanyWinId").window('close');
	    			}else{
	    				$.messager.alert("提示","删除失败","error");
	    			}
	    		}
	    })
	    }    
	}); 
	
}

//修改部门信息
function update(td){//td 1地区 2部门
	var title;
	if(td==1){
		var row = $('#dgArea').datagrid('getSelected');
		document.getElementById("bm").innerHTML="地区编码";
		document.getElementById("mc").innerHTML="地区名称";
		title="修改地区信息";
	}else if(td==2){
		var row = $('#dgDepartment').datagrid('getSelected');
		document.getElementById("bm").innerHTML="部门编码";
		document.getElementById("mc").innerHTML="部门名称";
		title="修改部门信息";
	}
	
	if(row==null){
		$.messager.alert("提示","请先选中一条记录","error");
		return;
	}
	 $("#departmentCodeId").textbox('setValue', row.departmentCode);
	 $("#departmentNameId").textbox('setValue', row.departmentName);
  	
  	$('#DepartmentWinId').dialog({
		title: title,
		width: 350,
		height: 250,
		closed: false,
		cache: false,
		modal: true,
		buttons:[{
			text:'修改',
			iconCls:'icon-edit',
			handler:function(){
			  	if($("#addForm").form('validate')){
			  		var departmentCode = $("#departmentCodeId").val();
			  		var departmentName = $("#departmentNameId").val().replace(/\s/g, "");
//			  		if(departmentCode==""){
//			  			$.messager.alert("友情提示","地区编码不允许为空","error");
//			  			return;
//			  		}
			  		if(departmentName==""){
			  			$.messager.alert("友情提示","地区名称不允许为空","error");
			  			return;
			  		}
			  		
			  		$.ajax({  
						url:'../../Areadepartment/updateAreaDepartment',
						type:'post',
						data:{
						"id":row.id,
						"departmentCode":departmentCode,
						"departmentName":departmentName,
						"type":typeid
						},
						dataType:'json',
						error:function(){
							$.messager.alert("提示","修改异常","error");
						}, 
						success:function(map){ 
							var row = map.row;
							if(row>=0){
								$.messager.alert("提示","修改成功","info");
								if(td==1){
									$('#dgArea').datagrid("reload");
								}else if(td==2){
									$('#dgDepartment').datagrid("reload");
								}
								
								$('#DepartmentWinId').dialog('close');
								return;
							}else{
								$.messager.alert("提示","添加失败","error");
								
								return;
							}
						}  
					});
			  	}
			    
			}
				
		},{
			text:'退出',
			iconCls:'icon-no',
			handler:function(){
				$('#DepartmentWinId').dialog('close');
			}
		}]
	});
}

//新增弹出窗
function companyWin(td){
	var id;
	if(td==1){
		id=1;
		document.getElementById("bm").innerHTML="地区编码";
		document.getElementById("mc").innerHTML="地区名称";
		title="新增地区信息";
	}else if(td==2){
		var dg = $('#dgArea').datagrid('getSelected');//地区列表
		id=dg.id;
		document.getElementById("bm").innerHTML="部门编码";
		document.getElementById("mc").innerHTML="部门名称";
		title="新增部门信息";
	}
     
     $("#departmentCodeId").textbox('setValue', "");
	 $("#departmentNameId").textbox('setValue', "");
		$('#DepartmentWinId').dialog({
			title: title,
			width: 350,
			height: 250,
			closed: false,
			cache: false,
			modal: true,
			buttons:[{
				text:'保存',
				iconCls:'icon-ok',
				handler:function(){
				  	if($("#addForm").form('validate')){
				  		var departmentCode = $("#departmentCodeId").val();
				  		var departmentName = $("#departmentNameId").val().replace(/\s/g, "");
//				  		if(departmentCode==""){
//				  			$.messager.alert("友情提示","部门编码不允许为空","error");
//				  			return;
//				  		}
				  		if(departmentName==""){
				  			$.messager.alert("友情提示","部门名称不允许为空","error");
				  			return;
				  		}
				  		
				  		$.ajax({  
							url:'../../Areadepartment/adddAreaDepartment',
							type:'post',
							data:{
				  			"parentId":id,
							"departmentCode":departmentCode,
							"departmentName":departmentName,
							"type":typeid,
							"isDel":'1'
							},
							dataType:'json',
							error:function(){
								$.messager.alert("提示","添加异常","error");
							}, 
							success:function(map){ 
								var row = map.row;
								if(row>=0){
									$.messager.alert("提示","添加成功","info");
									if(td==1){
										$('#dgArea').datagrid("reload");
									}else if(td==2){
										$('#dgDepartment').datagrid("reload");
									}
									$('#DepartmentWinId').dialog('close');
									return;
								}else{
									$.messager.alert("提示","添加失败","error");
									return;
								}
							}  
						});
				  	}
				    
				}
					
			},{
				text:'退出',
				iconCls:'icon-no',
				handler:function(){
					$('#DepartmentWinId').dialog('close');
				}
			}]
		});
} 


var statsUpload='1';

//启用/删除
function qidel(id,value){
	var url ='../../GsController/updateGsHtml';
	
	var state = "";
	var delName="";
	if(value=='1'){
		state='2';
		delName="隐藏";
	}
	if(value=='2'){
		state='1';
		delName="显示";
	} 
	$.messager.confirm('确认','您确认想要 [ '+delName+' ] 数据吗？',function(r){    
	    if (r){    
	    	$.ajax({  
				url:url,
				type:'post',
				data:{
				"id":id,
				"state":state
				},
				dataType:'json',
				error:function(){
					$.messager.alert("提示",delName+"异常","error");
				}, 
				success:function(map){ 
					var row = map.row;
					if(row>=0){
						$.messager.alert("提示",delName+"成功","info");
							$('#dgcontent').datagrid("reload");
						return;
					}else{
						$.messager.alert("提示",delName+"失败","error");
						return;
					}
				}  
			});    
	    }    
	});
}



Date.prototype.format = function(format){
			var o = {
			"M+" : this.getMonth()+1, //month
			"d+" : this.getDate(), //day
			"h+" : this.getHours(), //hour
			"m+" : this.getMinutes(), //minute
			"s+" : this.getSeconds(), //second
			"q+" : Math.floor((this.getMonth()+3)/3), //quarter
			"S" : this.getMilliseconds() //millisecond
			}

			if(/(y+)/.test(format)) {
			format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
			}

			for(var k in o) {
			if(new RegExp("("+ k +")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
			}
			}
			return format;
		}